package com.token.sysorg.services;
import java.sql.ResultSet;
import com.token.sql.IntegrateSql;
import com.token.sql.PrepareSql;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.token.base.FuncUtil;
import com.token.base.Pl;
/**
 * 读组织架构_返回tree的json结构
 * @author anger
 * @version 1.0
 */
public class FSysOrgRead_ForTree{
	private static JSONArray arr = new JSONArray();
	public JSONArray ReadOrg_ForTree(FuncUtil F,IntegrateSql iSql) throws Exception {
		String unitid = F.getUrlQueryString("id");
		if(unitid == null ||unitid.equals("")){
			unitid = "root";
			ReadUnit(F, iSql, unitid);
		}else if("C".equals(unitid.substring(0, 1).toUpperCase())){
			ReadUnit(F, iSql, unitid);
			ReadDepartment(F, iSql, unitid, "root");
		}else if("D".equals(unitid.substring(0, 1).toUpperCase())){
			String deptid = F.getUrlQueryString("id");
			String ComID = F.getUrlQueryString("ComID");
			ReadDepartment(F, iSql, ComID, deptid);
			ReadPersonnel(F, iSql, ComID, deptid);
		}
		return arr;
	}

	private static void ReadUnit(FuncUtil F,IntegrateSql iSql, String unitid) throws Exception {
		String unitsql = "select ComID,UnitID,UnitName,ParentID from F_Sys_Unit where ParentID=? and SysID=?";
		PrepareSql psql = new PrepareSql(iSql);
		psql.setPrepare(unitsql);
		psql.setPstValue(1, unitid);
		psql.setPstValue(2, Pl.getSysId());
		ResultSet rs = psql.executeQuery();
		while (rs.next()){
			JSONObject org = new JSONObject();
			org.put("id", rs.getString("UnitID"));
			org.put("title", rs.getString("UnitName"));
			org.put("value", rs.getString("UnitID"));
			org.put("pId", unitid);
			arr.add(org);
		}
	}

	private static void ReadDepartment(FuncUtil F,IntegrateSql iSql, String comid, String deptid) throws Exception {
		String unitsql = "select DeptID,DepartmentNumber,DepartmentName,ComID,ParentDeptID from F_Sys_Department where ComID=? and ParentDeptID=? and SysID=?";
		PrepareSql psql = new PrepareSql(iSql);
		psql.setPrepare(unitsql);
		psql.setPstValue(1, comid);
		psql.setPstValue(2, deptid);
		psql.setPstValue(3, Pl.getSysId());
		ResultSet rs = psql.executeQuery();
		while (rs.next()){
			JSONObject org = new JSONObject();
			org.put("id", rs.getString("DepartmentNumber"));
			org.put("title", rs.getString("DepartmentName"));
			org.put("value", rs.getString("DepartmentNumber"));
			org.put("ComID", rs.getString("ComID"));
			if("root".equals(deptid)){
				org.put("pId", comid);
			}else{
				org.put("pId", deptid);
			}

			arr.add(org);
		}
	}

	private static void ReadPersonnel(FuncUtil F,IntegrateSql iSql, String comid, String key) throws Exception {
		String unitsql = "select PersonID,PersonNumber,PersonName,ComID,DeptID from F_Sys_Personnel where ComID=? and DeptID=? and SysID=?";
		PrepareSql psql = new PrepareSql(iSql);
		psql.setPrepare(unitsql);
		psql.setPstValue(1, comid);
		psql.setPstValue(2, key);
		psql.setPstValue(3, Pl.getSysId());
		ResultSet rs = psql.executeQuery();
		while (rs.next()){
			JSONObject org = new JSONObject();
			org.put("id", rs.getString("PersonID"));
			org.put("title", rs.getString("PersonName"));
			org.put("value", rs.getString("PersonNumber"));
			org.put("ComID", rs.getString("ComID"));
			org.put("DeptID", rs.getString("DeptID"));
			org.put("pId", rs.getString("DeptID"));
			org.put("isLeaf", true);
			org.put("inco", "layui-icon-user");
			arr.add(org);
		}
	}
}
